<template>
  <!-- 表单部分 -->
  <el-form inline>
    <el-form-item label="商品名称：">
      <el-input v-model="queryForm.name" clearable />
    </el-form-item>
    <el-form-item label="所属分类：">
      <SubCategorySelect v-model="queryForm.subCategoryId" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="getGoodsPage">
        <el-icon>
          <Search />
        </el-icon>搜索
      </el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="addGoods">+新增商品</el-button>
      <el-button type="danger" @click="deleBatch">批量删除</el-button>
    </el-form-item>
  </el-form>

  <!-- 表格展示 -->
  <el-table ref="tableRef" v-loading="loading" :data="GoodsList" style="width: 100%">
    <el-table-column type="selection" width="55" />
    <el-table-column type="index" label="序号" width="100" />
    <el-table-column label="图片">
      <template #default="scope">
        <img :src="scope.row.picture" alt="图片" style="width: 100px; height: 100px" />
      </template>
    </el-table-column>
    <el-table-column prop="name" label="商品名称" />
    <el-table-column prop="subCategoryName" label="所属分类" />
    <el-table-column prop="price" label="价格" />
    <el-table-column prop="oldPrice" label="原价" />
    <el-table-column label="状态">
      <template #default="{ row }">
        <el-switch v-model="row.status" :active-value="1" :inactive-value="0"
          @change="goodsByStatus(row.status, row.id)" />
      </template>
    </el-table-column>
    <el-table-column prop="updateTime" label="更新时间" />
    <el-table-column label="操作" width="300">
      <template #default="{ row }">
        <el-button type="primary" @click="editGoods(row)">
          <el-icon>
            <Edit />
          </el-icon>
        </el-button>
        <el-popconfirm title="确认删除吗?" confirm-button-text="确认" cancel-button-text="取消" @confirm="deleGoods(row.id)">
          <template #reference>
            <el-button type="danger">
              <el-icon>
                <Delete />
              </el-icon>
            </el-button>
          </template>
        </el-popconfirm>
        <el-button type="primary" @click="getGoods(row)">
          详情
        </el-button>
        <el-button type="primary" @click="openSkuDialog(row.id, row.specList)">
          sku管理
        </el-button>
      </template>
    </el-table-column>
    <template #empty>
      <el-empty description="没有数据"></el-empty>
    </template>
  </el-table>

  <!-- 分页 -->
  <el-pagination v-model:current-page="queryForm.page" v-model:page-size="queryForm.pageSize"
    :page-sizes="[2, 5, 10, 20]" :background="true" layout="jumper, total, sizes, prev, pager, next" :total="total"
    @size-change="getGoodsPage" @current-change="getGoodsPage" />

  <GoodsEdit ref="dialog" @success="onSuccess" />
  <GoodsDrawer ref="drawer" />
  <Sku ref="skuDialog" />
</template>

<script setup>
// 引入依赖
import { ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { getGoodsPageAPI, deleGoodsAPI, goodsByStatusAPI } from "@/apis/goods.js";
import GoodsEdit from "./components/GoodsEdit.vue";
import SubCategorySelect from "./components/SubCategorySelect.vue";
import GoodsDrawer from "./components/GoodsDrawer.vue";
import Sku from "./components/Sku.vue";

// 定义响应式数据
const queryForm = ref({
  page: 1,
  pageSize: 10,
  name: "",
  subCategoryId: "",
  sortField: "sales_count",
});

const GoodsList = ref([]);
const loading = ref(false);
const total = ref(0);
const dialog = ref(null);
const tableRef = ref(null);
const drawer = ref(false);
const skuDialog = ref(false);

// 定义方法
const getGoodsPage = async () => {
  loading.value = true;
  const res = await getGoodsPageAPI(queryForm.value);
  GoodsList.value = res.data.records;
  total.value = res.data.total;
  loading.value = false;
};

const addGoods = () => {
  dialog.value.open({});
};

const deleGoods = async (id) => {
  const res = await deleGoodsAPI(id);
  if (res.code === 1) {
    ElMessage.success("删除成功");
    getGoodsPage();
  }
};

const editGoods = (row) => {
  dialog.value.open(row);
};

const getGoods = (row) => {
  drawer.value.open(row);
};

const openSkuDialog = (id, specList) => {
  skuDialog.value.open(id, specList);
};

const goodsByStatus = async (status, id) => {
  const res = await goodsByStatusAPI(status, id);
  if (res.code === 1) {
    ElMessage.success("修改状态成功");
    getGoodsPage();
  }
};

const onSuccess = async (isEdit) => {
  if (!isEdit) {
    const res = await getGoodsPageAPI(queryForm.value);
    const totalPages = Math.ceil(res.data.total / queryForm.value.pageSize);
    queryForm.value.page = totalPages;
  }
  getGoodsPage();
};

const deleBatch = () => {
  const selectedRows = tableRef.value.getSelectionRows();
  if (selectedRows.length === 0) {
    ElMessage.warning("没有选中的商品");
  } else {
    const selectedIds = selectedRows.map((row) => row.id).join(",");
    deleGoods(selectedIds);
  }
};

// 生命周期钩子
onMounted(() => {
  getGoodsPage();
});
</script>